package com.wang.springbootinit.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.springbootinit.common.ErrorCode;
import com.wang.springbootinit.exception.BusinessException;
import com.wang.springbootinit.mapper.TestEssentialsMapper;
import com.wang.springbootinit.model.dto.TestEssentialsAddRequest;
import com.wang.springbootinit.model.dto.TestEssentialsUpdateRequest;
import com.wang.springbootinit.model.entity.ResearchSubject;
import com.wang.springbootinit.model.entity.TestEssentials;
import com.wang.springbootinit.model.vo.TestEssentialsVO;
import com.wang.springbootinit.service.ResearchSubjectService;
import com.wang.springbootinit.service.TestEssentialsService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toMap;

/**
 * 考试要点服务实现类
 */
@Service
public class TestEssentialsServiceImpl extends ServiceImpl<TestEssentialsMapper, TestEssentials> implements TestEssentialsService {

    @Resource
    private TestEssentialsMapper testEssentialsMapper;

    @Autowired
    private ResearchSubjectService researchSubjectService;

    /**
     * 根据科目ID和类型获取考试要点列表
     * @param subjectId 科目ID
     * @param type 要点类型
     * @return 考试要点列表
     */
    @Override
    public List<TestEssentialsVO> getTestEssentialsList(Long subjectId, Integer type) {
        LambdaQueryWrapper<TestEssentials> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TestEssentials::getSubjectId, subjectId);
        if (type != null) {
            queryWrapper.eq(TestEssentials::getType, type);
        }
        queryWrapper.eq(TestEssentials::getIsDeleted, 0);
        List<TestEssentials> testEssentials = testEssentialsMapper.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(testEssentials)){
            return new ArrayList<>();
        }
        List<Long> subjectIds = testEssentials.stream().map(TestEssentials::getSubjectId).collect(Collectors.toList());
        Map<Long ,ResearchSubject> subjectMap = new HashMap();
        if(CollectionUtils.isNotEmpty(subjectIds)){
            List<ResearchSubject> researchSubjects = researchSubjectService.listByIds(subjectIds);
            if(CollectionUtils.isNotEmpty(researchSubjects)){
                subjectMap = researchSubjects.stream().collect(toMap(ResearchSubject::getId,ResearchSubject -> ResearchSubject, (k1, k2) -> k1));
            }
        }
        Map<Long, ResearchSubject> finalSubjectMap = subjectMap;
        return testEssentials.stream().map(item -> {
            TestEssentialsVO testEssentialsVO = new TestEssentialsVO();
            BeanUtils.copyProperties(item,testEssentialsVO);
            testEssentialsVO.setResearchSubject(finalSubjectMap.get(item.getSubjectId()));
            return testEssentialsVO;
        }).collect(Collectors.toList());
    }

    /**
     * 添加考试要点
     * @param request 添加要点请求对象
     * @return 新创建的要点ID
     */
    @Override
    public Long addTestEssentials(TestEssentialsAddRequest request) {
        TestEssentials testEssentials = new TestEssentials();
        BeanUtils.copyProperties(request, testEssentials);
        testEssentials.setCreateTime(new Date());
        testEssentials.setUpdateTime(new Date());
        testEssentialsMapper.insert(testEssentials);
        return testEssentials.getId();
    }

    /**
     * 更新考试要点
     * @param request 更新要点请求对象
     * @return 是否更新成功
     */
    @Override
    public Boolean updateTestEssentials(TestEssentialsUpdateRequest request) {
        TestEssentials testEssentials = new TestEssentials();
        BeanUtils.copyProperties(request, testEssentials);
        testEssentials.setUpdateTime(new Date());
        return testEssentialsMapper.updateById(testEssentials) > 0;
    }

    /**
     * 逻辑删除考试要点
     * @param id 要点ID
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteTestEssentials(Long id) {
        if(id == null){
            throw new BusinessException("id不能为空");
        }
        TestEssentials testEssentials = getById(id);
        if(ObjectUtil.isNull(testEssentials)){
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return removeById(id);
    }
} 